//
//  ST_DTSnapChatCell.swift
//  btvideo
//
//  Created by mac on 2020/10/15.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SDWebImage


class ST_DTSnapChatCell: ST_DTChatCell {
    
    fileprivate lazy var Btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.addTarget(self, action: #selector(display_full_image), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    
    @objc func display_full_image(){
        
        if let check = message.passedCheck , check == false{
            return
        }
        
        guard let msg = message, msg.isSnapDestroy == false else { return  }
        
        let vc = ST_SnapchatBrowserController()
        
        vc.dataMsg = msg
        
        GlobalPresent(toController: vc)
        
    }
    
    
    
    
}



//MARK: - Left
class ST_DTSnapChatLeftCell: ST_DTSnapChatCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var snapchat_image_view:ST_SnapChatImageLeftBoxControl = {
        let control = ST_SnapChatImageLeftBoxControl()
        control.isUserInteractionEnabled = false
        return control
    }()
    
    override func commonInit() {
        super.commonInit()
        
        contentView.addSubview(snapchat_image_view)
        
        contentView.addSubview(Btn)
        
        
        
        Btn.snp.makeConstraints { (make) in
            make.edges.equalTo(snapchat_image_view)
        }
        
        icon.snp.makeConstraints { (make) in
            make.width.height.equalTo(DTChatUserImageWidth)
            make.left.top.equalToSuperview().offset(15)
        }
        
        
        snapchat_image_view.snp.makeConstraints { (make) in
            make.height.width.equalTo(140)
            make.left.equalTo(icon.snp.right).offset(10)
            make.top.equalTo(icon)
        }
        
        
        self.contentView.addSubview(tailView)
        tailView.snp.makeConstraints { (make) in
            make.height.equalTo(17)
            make.top.equalTo(snapchat_image_view.snp.bottom).offset(5)
            make.left.equalTo(snapchat_image_view)
        }
        
    }
    
    fileprivate lazy var tailView:ST_ChatMessageTailControl = {
         let control = ST_ChatMessageTailControl()
          return control
      }()
  
    override var message: ST_DTChatMessage!{
        didSet{
            
                icon.pin_setImage(from: message.portraitUri.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
                
                self.uuid = message.uuid
                
                snapchat_image_view.data = message
              
            tailView.model = self.message.tail
            
        }
    }
    
}

//MARK: - right
class ST_DTSnapChatRightCell: ST_DTSnapChatCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var snapchat_image_view:ST_SnapChatImageRightBoxControl = {
        let control = ST_SnapChatImageRightBoxControl()
        control.isUserInteractionEnabled = false
        return control
    }()
    
    override func commonInit() {
        super.commonInit()
        
        contentView.addSubview(snapchat_image_view)
        
        contentView.addSubview(Btn)
        
        
        
        Btn.snp.makeConstraints { (make) in
            make.edges.equalTo(snapchat_image_view)
        }
        
        snapchat_image_view.snp.makeConstraints { (make) in
            make.height.width.equalTo(140)
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview().offset(15)
        }
        
        
        contentView.addSubview(upload_error_btn)
        upload_error_btn.snp.makeConstraints { (make) in
            make.right.equalTo(snapchat_image_view.snp.left).offset(-10)
            make.centerY.equalTo(snapchat_image_view)
            make.width.height.equalTo(ST_DTChatCell.uploadErrorBtnSize)
        }
        
        
        
        contentView.addSubview(uploadIndicator)
        uploadIndicator.snp.makeConstraints { (make) in
            make.centerY.equalTo(snapchat_image_view)
            make.right.equalTo(snapchat_image_view.snp.left).offset(-8)
        }
    }
    
   
    override func setting_uplaod_status() {
        
        super.setting_uplaod_status()
        
        if message.uploadStatus == .uploading {
            
            uploadIndicator.startAnimating()
            
        }else{
            uploadIndicator.stopAnimating()
        }
        
        upload_error_btn.isHidden = message.uploadStatus != .failure
    }
    
    override var message: ST_DTChatMessage!{
        didSet{
            
                icon.pin_setImage(from: message.portraitUri.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
                
                self.uuid = message.uuid
                
                snapchat_image_view.data = message
              
            
        }
    }
}


//MARK: - Image Box
fileprivate class ST_SnapChatImageBoxControl: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(chat_image_view)
        chat_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(open_mask_view)
        open_mask_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        open_mask_view.addSubview(open_status_icon_view)
        open_status_icon_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(34)
            make.width.height.equalTo(30)
        }
        
        
        open_mask_view.addSubview(open_title_label)
        open_title_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(69)
            make.height.equalTo(20)
        }
        
        open_mask_view.addSubview(open_content_label)
        open_content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(90)
            make.height.equalTo(17)
        }
        
        
        self.addSubview(close_mask_view)
        close_mask_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        close_mask_view.addSubview(close_status_icon_view)
        close_status_icon_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(43)
            make.width.height.equalTo(30)
        }
        
        
        close_mask_view.addSubview(close_title_label)
        close_title_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(78)
            make.height.equalTo(20)
        }
        
        
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var chat_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        return control
    }()
    
    var data:ST_DTChatMessage?{
        didSet{
            
            guard let message = data else { return  }
            
            if let check = message.passedCheck, check == false{
                
                close_title_label.text = "图片违规"
                
                chat_image_view.image = nil
                
                self.isOpen = check
                
                return
                
            }else{
                
                close_title_label.text = "图片已焚毁"
                
            }
            
            
            //            判断 本地有没有这个图片
            if let localPath = message.thumbnailImageLocalPath,FileManager.default.fileExists(atPath: localPath),let localImage = localPath.load_image_from_disk(){
                
                chat_image_view.image = localImage
                
            }else {
                
                chat_image_view.sd_setImage(with: message.imageRemoteUrl?.url, completed: nil)
            }
            
            
            self.isOpen = !message.isSnapDestroy
            
        }
    }
    //    MARK: - Snap chat Status
    var isOpen = true{
        didSet{
            open_mask_view.isHidden = !isOpen
            close_mask_view.isHidden = isOpen
        }
    }
    
    fileprivate lazy var open_mask_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xFEC207, transparency: 0.9)
        return control
    }()
    
    fileprivate lazy var open_status_icon_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("snapChat_open_icon"))
        control.contentMode = .scaleAspectFill
        return control
    }()
    
    fileprivate lazy var open_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "阅后即焚"
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var open_content_label:UILabel = {
        let control = UILabel.init()
        control.text = "点击查看图片"
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var close_mask_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0x373B40, transparency: 0.9)
        control.isHidden = true
        control.cornerRadius = 15
        return control
    }()
    
    fileprivate lazy var close_status_icon_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("snapChat_close_icon"))
        control.contentMode = .scaleAspectFill
        return control
    }()
    
    fileprivate lazy var close_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "图片已焚毁"
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    //    MARK: - 暂时不用 进度条
    fileprivate lazy var uploading_mas_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.gray
        control.alpha = 0.8
        control.cornerRadius = 15
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var indicator:UIActivityIndicatorView = {
        
        let control = UIActivityIndicatorView()
        
        return control
        
    }()
    fileprivate lazy var indicator_label:UILabel = {
        
        let control = UILabel()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 12)
        control.textAlignment = .center
        return control
    }()
    
    
    
}


fileprivate class ST_SnapChatImageLeftBoxControl:ST_SnapChatImageBoxControl{
    
    
    fileprivate lazy var drawRoundCorners = false
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        print("一个小Bug:",self.width,"---")
        
        if !drawRoundCorners,self.width > 50{
            drawRoundCorners = true
            self.roundCorners([UIRectCorner.topRight,UIRectCorner.bottomLeft,UIRectCorner.bottomRight], radius: 20)
        }
    }
    
}




fileprivate class ST_SnapChatImageRightBoxControl:ST_SnapChatImageBoxControl{
    
    
    fileprivate lazy var drawRoundCorners = false
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        print("一个小Bug:",self.width,"---")
        
        if !drawRoundCorners,self.width > 50{
            drawRoundCorners = true
            self.roundCorners([UIRectCorner.topLeft,UIRectCorner.bottomLeft,UIRectCorner.bottomRight], radius: 20)
        }
    }
    
}
